﻿using System;

namespace YXYLCommon
{
    [Serializable]
    public class DbDecimal
    {
        public DbDecimal()
        { 
            
        }

        private DbDecimal(object value)
        {
            if (value == Convert.DBNull||Convert.ToString(value)=="")
            {
                this.dvalue = dvalueDBNull;
                this.dvalueDBParameter = Convert.DBNull;
            }
            else
            {
                this.dvalue = Convert.ToDecimal(value);
                this.dvalueDBParameter = this.dvalue;
            }
        }
        private DbDecimal(string value)
        {
            decimal valueTemp;
            if (value == string.Empty||value==null||decimal.TryParse(value,out valueTemp)==false)
            {
                this.dvalue = dvalueDBNull;
                this.dvalueDBParameter = Convert.DBNull;
            }
            else
            {
                this.dvalue =valueTemp;
                this.dvalueDBParameter =valueTemp;
            }
        }
        private DbDecimal(decimal value)
        {
            this.dvalue = value;
            this.dvalueDBParameter = value;
        }  

        private object dvalueDBParameter = Convert.DBNull;
        private decimal dvalue=dvalueDBNull;
        private static readonly decimal dvalueDBNull = decimal.MinValue;

        public decimal Value
        {
            get { return dvalue; }
            set { dvalue = value; }
        }
        /// <summary>
        /// 用于执行存储过程需要的参数
        /// </summary>
        public object DvalueDBParameter
        {
            get { return dvalueDBParameter; }
        }

        public bool IsDBNull
        {
            get
            {
                if (this.dvalue == dvalueDBNull)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        
        public override string ToString()
        {
            string str = string.Empty;
            if (this.dvalue != dvalueDBNull)
            {
                str = this.dvalue.ToString();
            }
            return str;
        }
        public string ValueString
        {
            get
            {
                if (this.dvalue == dvalueDBNull)
                {
                    return string.Empty;
                }
                else
                {
                    return this.dvalue.ToString("0.00");
                }
            }
        }

        public string ToString(string format)
        {
            if (this.dvalue == dvalueDBNull)
            {
                return string.Empty;
            }
            else
            {
                return this.dvalue.ToString(format);
            }
        }

        public static DbDecimal Parse(object value)
        {
            return new DbDecimal(value);
        }
        public static DbDecimal Parse(string value)
        {
            return new DbDecimal(value);
        }
        public static DbDecimal Parse(DateTime value)
        {
            return new DbDecimal(value);
        }

        
    }

}
